#include "qteguiglobal.h"
#include <QTECore/qteglobal.h>

#include <QApplication>
#include <QAbstractItemView>
#include <QWidget>

namespace QTE
{

namespace Gui
{

// 设置字体
QTEGUI_EXPORT void setFont(QWidget *widget, const QFont &font)
{
	auto &list = J_CCT(QObjectList&, widget->children());
	for(auto &obj : list)
	{
		auto widget = J_OCT(QWidget*, obj);
		if( widget != nullptr )
		{
			setFont(widget, font); //递归
			widget->setFont(font);
		}
	}
}

// 设置文本颜色（调色板）
QTEGUI_EXPORT void setTextColor(QWidget *widget, const QColor &color)
{
	auto palette = widget->palette();
	palette.setColor(QPalette::WindowText, color);
	widget->setPalette(palette);
	widget->update();
}

// 设置背景颜色（调色板）
QTEGUI_EXPORT void setBackgroundColor(QWidget *widget, const QColor &color)
{
	auto palette = widget->palette();
	palette.setColor(QPalette::Window, color);
	widget->setStyleSheet("");
	widget->setAutoFillBackground(true);
	widget->setPalette(palette);
	widget->update();
}

// 设置窗口背景图
QTEGUI_EXPORT void setBackgroundPictrue(QWidget *widget, const QString &path)
{
	QPixmap pixmap(path);
	auto palette = widget->palette();
	palette.setBrush(widget->backgroundRole(), QBrush(pixmap));
	widget->setStyleSheet("");
	widget->setAutoFillBackground(true);
	widget->setPalette(palette);
	widget->update();
}

// 设置窗口背景图
QTEGUI_EXPORT void setBackgroundPictrue(QWidget *widget, const QString &path, const QSize &size)
{
	QPixmap pixmap(path);
	auto palette = widget->palette();
	palette.setBrush(widget->backgroundRole(), QBrush(pixmap.scaled(size)));
	widget->setStyleSheet("");
	widget->setAutoFillBackground(true);
	widget->setPalette(palette);
	widget->update();
}

// 获取widget的全局坐标
QTEGUI_EXPORT QPoint screenPos(const QWidget *widget)
{
	float px = 0;
	float py = 0;
	auto parent = widget;
	const QWidget *preParent = nullptr;

	do
	{
		QPoint p = parent->pos();
		px += p.x();
		py += p.y();
		preParent = parent;
		parent = parent->parentWidget();

	} while( parent != nullptr );

	QSize size = preParent->frameSize();
	QSize size2 = preParent->size();

	px += (size.width() - size2.width()) >> 1;
	py += size.height() - size2.height() - ((size.width() - size2.width()) >> 1);

	return QPoint(px, py);
}

Q_DECL_HIDDEN void __grabGesture(QWidget *widget, QScroller::ScrollerGestureType type)
{
	auto scroller = QScroller::scroller(widget);
	scroller->grabGesture(widget, type);

	auto properties = scroller->scrollerProperties();

	properties.setScrollMetric(QScrollerProperties::MousePressEventDelay, 0);
	properties.setScrollMetric(QScrollerProperties::DragStartDistance, 0);
	properties.setScrollMetric(QScrollerProperties::DragVelocitySmoothingFactor, 1);
	properties.setScrollMetric(QScrollerProperties::SnapTime, 2.5);
	properties.setScrollMetric(QScrollerProperties::FrameRate, QScrollerProperties::Fps60);

	scroller->setScrollerProperties(properties);

	auto aiv = J_OCT(QAbstractItemView*, widget);

	if( aiv != nullptr )
	{
		aiv->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
		aiv->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
	}
}

QTEGUI_EXPORT void grabGesture(QWidget *widget, QScroller::ScrollerGestureType type)
{
	if( J_OCT(QAbstractScrollArea*, widget) )
		__grabGesture(widget, type);

	auto &objectList = J_CCT(QObjectList&, widget->children());

	for(auto *obj : objectList)
	{
		auto cwgt = J_OCT(QWidget*, obj);
		if( cwgt == nullptr )
			continue;

		if( J_OCT(QAbstractScrollArea*, cwgt) )
			__grabGesture(cwgt, type);

		grabGesture(cwgt, type);
	}
}

} //namespace Gui

} //namespace JXQ
